package com.ipan.weixin.msg.general;

import org.apache.commons.lang.StringUtils;
import org.dom4j.Document;
import org.dom4j.Element;

/**
 * 事件消息
 * 
 * @author iPan
 * @version 2013-12-23
 */
abstract public class EventMsg extends AbstractXmlMsg {

	/**
	 * serialVersionUID
	 */
	private static final long serialVersionUID = 746925032898420623L;
	
	/** 事件类型 */
	protected String event;
	
	public EventMsg() {
		super();
	}

	public EventMsg(String toUserName, String fromUserName, String createTime, String msgType, TransportType transportType) {
		super(toUserName, fromUserName, createTime, msgType, transportType);
	}

	public EventMsg(TransportType transportType) {
		super(transportType);
	}

	public String getEvent() {
		return event;
	}

	public void setEvent(String event) {
		this.event = event;
	}
	
	/**
	 * 接收XML消息
	 */
	@Override
	public void fromXml(Document doc) {
		super.fromXml(doc);
		Element root = doc.getRootElement();
		this.event = root.elementTextTrim(EL_EVENT);
	}
	
	@Override
	public String toXml(boolean nonEmpty) {
		StringBuilder buf = new StringBuilder();
		buf.append(super.toXml(nonEmpty));
		if (nonEmpty) {
			if (StringUtils.isNotEmpty(this.event)) {
				buf.append(wrapElementHead(EL_EVENT)).append(this.event).append(wrapElementEnd(EL_EVENT));
			}
		} else {
			buf.append(wrapElementHead(EL_EVENT)).append(getStringValue(this.event)).append(wrapElementEnd(EL_EVENT));
		}
		return buf.toString();
	}

}
